﻿using OrderApp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderUI {
    public partial class OrderDetailForm : Form {
        private Order order;
        public OrderDetailForm() {
            InitializeComponent();
        }
        /// <param name="or">out参数,用于在两个Form之间传递数据，添加订单时传入的只有date，修改订单传入的是原来的订单</param>
        public OrderDetailForm(Order or, string text) {
            InitializeComponent();
            order = or;
            this.Text = text;
            initControls();
        }
        //数据绑定之类初始化成员的操作
        private void initControls() {
            detailBindingSource.DataSource = order.Details;
            detailGridView1.DataSource = detailBindingSource;
            detailGridView1.AutoGenerateColumns = false;

            if (this.Text == "修改订单") {
                orderIdText.Enabled = false;
                orderIdText.Text = order.Id.ToString();
            }
            timeLable.Text = order.CreateTime.ToString();
            List<Customer> customers = new List<Customer>() { order.Customer };
            customerCombox.DataSource = customers;
        }
        private void UpdateDetailsGrid() {
            detailGridView1.DataSource = order.Details;
        }
        //修改订单
        private void saveOrderBtn_Click(object sender, EventArgs e) {
            try {
                switch (this.Text) {
                    case "修改订单":
                        MessageBox.Show("修改成功");
                        break;
                    case "添加订单":
                        MessageBox.Show("请确认订单ID是否与之前的订单重复，若重复则会添加失败");
                        break;
                }
                this.Close();
            }
            catch (Exception ex) {
                MessageBox.Show("未知异常:" + ex.Message);
            }
        }

        private void orderIdText_TextChanged(object sender, EventArgs e) {
            order.Id = int.Parse(orderIdText.Text);
        }

        private void customerCombox_SelectedIndexChanged(object sender, EventArgs e) {

        }

        private void addDetailBtn_Click(object sender, EventArgs e) {
            OrderDetail detail = new OrderDetail();
            MyBoolean confirm = new MyBoolean(false);

            new DetailsForm(detail, confirm, "添加明细").ShowDialog();
            if (!confirm.Value)
                return;
            detailGridView1.Refresh();
            order.AddDetails(detail);
            UpdateDetailsGrid();
        }

        private void changeDetailBtn_Click(object sender, EventArgs e) {
            if (detailGridView1.SelectedRows.Count != 1) {
                MessageBox.Show("必须选择一个明细才能修改");
                return;
            }
            var de = detailGridView1.SelectedRows[0].DataBoundItem as OrderDetail;
            var index = detailGridView1.SelectedRows[0].Index;
            MyBoolean confirm = new MyBoolean(false);
            new DetailsForm(de, confirm, "修改明细").ShowDialog();
            if (!confirm.Value) return;
            order.RemoveDetails(index);
            order.AddDetails(de);
            UpdateDetailsGrid();
        }

        private void deletDetailBtn_Click(object sender, EventArgs e) {
            if (detailGridView1.Rows.Count == 0) {
                MessageBox.Show("订单明细已空", "Note");
                return;
            }
            DialogResult res = MessageBox.Show("确定要删除选中吗？", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res == DialogResult.Cancel)
                return;
            var selectRows = detailGridView1.SelectedRows;
            for (int i = selectRows.Count - 1; i >= 0; i--) {
                detailGridView1.Rows.Remove(selectRows[i]);
            }
        }

        private void detailGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            Goods good;
            switch (detailGridView1.Columns[e.ColumnIndex].Name) {
                case "Index":
                    good = e.Value as Goods;
                    e.Value = good?.Id;
                    break;
                case "Goods":
                    good = e.Value as Goods;
                    e.Value = good?.Name;
                    break;
                case "Price":
                    good = e.Value as Goods;
                    e.Value = good?.Price;
                    break;
            }
        }

        private void detailGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }
    }
}
